package TUILayer;
import java.util.Scanner;
import ControllerLayer.*;

public class StockUI
{   
    private StockController stockController;
    
    public StockUI()
    {
        stockController= StockController.getInstance();
    }
    
    public void start()
    {
        stockMenu();
    }
    
    private void stockMenu()
    {
       boolean exit = false;
       while(!exit)
       {
          int choise = writeStockMenu(); 
          if(choise == 1)
          { 
              addSupplier();
          }
          else{
              if(choise == 2){
                   addItem();
              }  
              else{
                  if(choise == 3){
                      checkStock();
                  }  
                    else{
                        if(choise == 4){
                            searchBestSupplier();
                        }
                        else{
                            if(choise ==5){
                                orderProducts();
                            }else{
                                exit = true;
                            }
                        }
                        }
                    }
                }
            
       }
    }
    private int writeStockMenu()
    {     
            Scanner keyboard = new Scanner(System.in);
            System.out.println("\f *** Stock Controlling Menu ***");
            System.out.println(" (1) Add supplier");
            System.out.println(" (2) Add new items into the stock");
            System.out.println(" (3) Check the stock");
            System.out.println(" (4) Search for the best supplier");
            System.out.println(" (5) Order products");
            System.out.println(" (6) Return");
            System.out.print("\n Make your choice: ");
            
            int choise = keyboard.nextInt();
            
            return choise;
        }
    
    private void checkStock()
    {
        System.out.println(stockController.checkStock());
        inputContinue(); 
    }
    
    private void addItem(){
        String productTitle = productTitle();
        String barCode = inputBarCode();
        double price = inputPriceAmount();
        int quantity = inputAmount();
        int minimumAmount = inputMinimumAmount();
        int maxinimumAmount = inputMaximumAmount();
        if(stockController.addItem(productTitle,barCode,price,quantity, minimumAmount, maxinimumAmount)==true){
            System.out.println("Item was successfully added.");
        }else{
            System.out.println("Such item already exists");
        }
        inputContinue();
    }
    
    private void addSupplier(){
        String name = inputName();
        String address = inputAddress();
        String productTitle = productTitle();
        String productCode =  inputBarCode();
        double productPrice = inputPriceAmount();
        
        stockController.addSupplier(name, address,productTitle, productCode, productPrice);
    }
    
    private void orderProducts(){
        int amount = inputAmount();
        stockController.createRequest(amount);
        System.out.println(stockController.printRequest());
        String answer = confirmation();
        if(answer.equals("y")||answer.equals("Y")){
            stockController.updateItem(amount);
            System.out.println("Products have been successfully ordered and the stock has bee updated.");
            inputContinue();
        }
        else
        {
            stockController.clearOrder();
            System.out.println("Order has been deleted.");
            inputContinue();
        }
    }
    
    
    private void searchBestSupplier(){
        System.out.println(stockController.searchBestSupplier());
        inputContinue();
    }
    
    
    private void inputContinue()
    {
         Scanner keyboard = new Scanner(System.in);  
         System.out.println(" Press Enter to continue:  ");
         String code = keyboard.nextLine();
    }
    private String inputName()
    {
         Scanner keyboard = new Scanner(System.in);  
         System.out.println(" Write supplier's name:  ");
         String name = keyboard.nextLine();
         return name;
    }
    private int inputAmount()
    {
        boolean ok = false;
        int amount = 0;
        while(!ok)
        {
           Scanner keyboard = new Scanner(System.in);
           System.out.println("Write amount of product:  ");
           try{
             amount = keyboard.nextInt();
             ok = true;
           }
           catch (Exception e)
            {
             System.out.println("It should be a number of products-  try again");
             String input = keyboard.nextLine();
           }
        }
        return amount;
    } 
    private String  inputBarCode()
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Input product code: ");
        String barCode = keyboard.nextLine();
        return barCode;
    }
     private String  inputAddress()
    {
        // makes an object keyboard to read input from the console
        Scanner keyboard = new Scanner(System.in);
        System.out.println(" Write Address: ");
        String address = keyboard.nextLine();
        return address;
    }
    private String productTitle()
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Input product title: ");
        String title = keyboard.nextLine();
        return title;
    }
    private double inputPriceAmount()
    {
        boolean ok = false;
        double amount = 0;
        while(!ok)
        {
           Scanner keyboard = new Scanner(System.in);
           System.out.println("Write price of the product:  ");
           try{
             amount = keyboard.nextDouble();
             ok = true;
           }
           catch (Exception e)
            {
             System.out.println("It should be a amount of money -  try again");
             String input = keyboard.nextLine();
           }
        }
        return amount;
    } 
    private int inputMinimumAmount()
    {
        boolean ok = false;
        int amount = 0;
        while(!ok)
        {
           Scanner keyboard = new Scanner(System.in);
           System.out.println("Write minimum amount of product possible:  ");
           try{
             amount = keyboard.nextInt();
             ok = true;
           }
           catch (Exception e)
            {
             System.out.println("It should be a number of products -  try again");
             String input = keyboard.nextLine();
           }
        }
        return amount;
    } 
    private int inputMaximumAmount()
    {
        boolean ok = false;
        int amount = 0;
        while(!ok)
        {
           Scanner keyboard = new Scanner(System.in);
           System.out.println("Write maximum amount of product possible:  ");
           try{
             amount = keyboard.nextInt();
             ok = true;
           }
           catch (Exception e)
            {
             System.out.println("It should be a number of products-  try again");
             String input = keyboard.nextLine();
           }
        }
        return amount;
    } 
    
     private String confirmation()
    {
        Scanner keyboard = new Scanner(System.in);
        System.out.println("Confirm the order? (y/n): ");
        String address = keyboard.nextLine();
        return address;
    }
}